package com.dup.jxlh.admin.controller;

import com.dup.jxlh.admin.entities.auth.AuthUser;
import com.dup.jxlh.admin.entities.model.Admin;
import com.dup.jxlh.admin.service.AdminService;
import com.dup.jxlh.admin.validator.AdminId;
import com.dup.jxlh.api.entities.CommonResult;
import com.dup.jxlh.api.entities.PaginationData;
import com.dup.jxlh.api.enums.ResultEnum;
import com.dup.jxlh.api.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigInteger;

@RestController
@RequestMapping("/admin")
@Validated
public class AdminController {
    @Autowired
    private AdminService adminService;

    @PostMapping("/getMyInfo")
    public CommonResult getMyInfo(@AuthenticationPrincipal AuthUser authUser) {
        return ResultUtil.result(ResultEnum.SUCCESS, adminService.getAdminInfo(authUser.getId()));
    }

    @PostMapping("/updateMyInfo")
    public CommonResult updateMyInfo(@AuthenticationPrincipal AuthUser authUser, @Validated Admin admin) {
        admin.setId(authUser.getId());
        admin.setCompanyId(null);
        admin.setDepartmentId(null);
        admin.setRoleId(null);
        admin.setLevel(null);
        admin.setEnable(null);
        if (adminService.updateAdminInfo(admin)) {
            return ResultUtil.result(ResultEnum.SUCCESS);
        }
        return ResultUtil.result(ResultEnum.ERROR);
    }

    @PostMapping("/updateMyPassword")
    public CommonResult updateMyPassword(
            @AuthenticationPrincipal AuthUser authUser,
            @RequestParam("oldPassword") String oldPassword,
            @RequestParam("newPassword") String newPassword) {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        BigInteger adminId = authUser.getId();
        boolean bMatch = bCryptPasswordEncoder.matches(oldPassword,
                adminService.getAuthInfo(adminId).getPassword());
        if (!bMatch) {
            return ResultUtil.result(ResultEnum.PASSWORD_ERROR);
        }
        newPassword = bCryptPasswordEncoder.encode(newPassword);
        if (adminService.updateAdminPassword(adminId, newPassword)) {
            return ResultUtil.result(ResultEnum.SUCCESS);
        }
        return ResultUtil.result(ResultEnum.ERROR);
    }

    @PostMapping("/getAdminList")
    @PreAuthorize("hasAnyAuthority('ROLE_Admin','ROLE_UserList_Show')")
    public CommonResult getAdminList(PaginationData paginationData, @Validated Admin admin) {
        return ResultUtil.result(ResultEnum.SUCCESS,
                adminService.getAdminList(paginationData, admin));
    }

    @PostMapping("/addAdminInfo")
    @PreAuthorize("hasAnyAuthority('ROLE_Admin','ROLE_UserList_Operate')")
    public CommonResult addAdminInfo(@Validated Admin admin) {
        admin.setEnable(true);
        if (adminService.addAdminInfo(admin)) {
            return ResultUtil.result(ResultEnum.SUCCESS);
        }
        return ResultUtil.result(ResultEnum.ERROR);
    }

    @PostMapping("/updateAdminInfo")
    @PreAuthorize("hasAnyAuthority('ROLE_Admin','ROLE_UserList_Operate')")
    public CommonResult updateAdminInfo(@Validated Admin admin) {
        admin.setCompanyId(null);
        if (adminService.updateAdminInfo(admin)) {
            return ResultUtil.result(ResultEnum.SUCCESS);
        }
        return ResultUtil.result(ResultEnum.ERROR);
    }

    @PostMapping("/updateAdminPassword")
    @PreAuthorize("hasAnyAuthority('ROLE_Admin','ROLE_UserList_Operate')")
    public CommonResult updateAdminPassword(@RequestParam("adminId") @AdminId BigInteger adminId) {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String password = bCryptPasswordEncoder.encode("123456");
        if (adminService.updateAdminPassword(adminId, password)) {
            return ResultUtil.result(ResultEnum.SUCCESS);
        }
        return ResultUtil.result(ResultEnum.ERROR);
    }

    @PostMapping("/enableAdmin")
    @PreAuthorize("hasAnyAuthority('ROLE_Admin','ROLE_UserList_Operate')")
    public CommonResult enableAdmin(@Validated Admin admin) {
        admin.setCompanyId(null);
        admin.setDepartmentId(null);
        admin.setRoleId(null);
        admin.setLevel(null);
        admin.setRealname(null);
        if (adminService.updateAdminInfo(admin)) {
            return ResultUtil.result(ResultEnum.SUCCESS);
        }
        return ResultUtil.result(ResultEnum.ERROR);
    }

}
